<?php
    class block_categoryMenuBlock_controller extends controllerBase
    {
        public function process()
        {
            if(!caching::hasCache('aCategories'))
            {
                $aMenus = caching::createCacheMenu();       
            }
            else
            {
                $aMenus = caching::getCache('aCategories');
            }

            $this->aParams['aMenus'] = $aMenus;
            $currentUri = '';
            if($this->sController == 'category')
            {
                $currentUri = URL::getLink('category/id/'.$this->iId);
            }
            else if($this->sController == 'product')
            {
                $currentUri = $this->get('sCategoryUri');
            }
            if($currentUri != '')
            {
                $rootUri = $aMenus['pRoot'][$currentUri]['uri'];
                $this->aParams['currentUri'] = $currentUri;
                $this->aParams['rootUri'] = $rootUri;
            }

        }

        public function createMenu($aMenus = array(),$bIsDid = true)
        {
            $aProducers = DBObject::getProducers();
            echo '<ul>';
            foreach($aMenus['child'] as $aCategory)
            {
                if($this->get('rootUri') == $aCategory['uri'])
                {
                    echo '<li class="active_category">';
                }
                else{
                    if($this->get('currentUri') == $aCategory['uri'])
                    {
                        echo '<li class="current_category">';
                    }
                    else
                    {
                        echo '<li>';
                    }
                }

                echo '<a href="'.$aCategory['uri'].'">'.$aCategory['name'].'</a>';
                if(isset($aCategory['child']))
                {
                    echo '<img class="'.$aCategory['sId'].'_img'.'" src="'.URL::getLink('upload/'.$aCategory['image']).'">';
                    $this->createMenu($aCategory,false);
                }
                echo '</li>';
            }

            if($bIsDid)
            {
                if(count($aProducers))
                {
                    echo '<li class="producer_menu">';
                    echo '<a href="javascript:void(0);">Nhà Cung cấp</a>';
                    echo '<ul>';
                    foreach($aProducers as $aProducer)
                    {
                        echo '<li><a href="'.URL::getLink('provider/view/'.$aProducer['producer_alias']).'">'.$aProducer['producer_name'].'</a><li>';
                    }
                    echo '</ul>';
                    echo '</li>';
                }
            }

            echo '</ul>';
        }
    }
?>